package cn.youframe.frame.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * User: Administrator
 * DateTime: 13-2-3: 下午8:44
 */
public class DateUtil {

    /**
     * 根据日期类型返回年月日(yyyyMMdd)
     * @param date
     * @return String
     */
    public static String getYMDByDate(String aMask, Date date) {
        SimpleDateFormat df = new SimpleDateFormat(aMask);
        return df.format(date);
    }

    /**
     * 转换字符串为日期类型
     * @param aMask
     * @param strDate
     * @return String
     */
    public static Date convertStringToDate(String aMask, String strDate) {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);
        try {
            date = df.parse(strDate);
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    /**
     * 转换字符串为日期类型
     * @param time
     * @return String
     */
    public static Date convertLongToDate(long time){
        Date date=new Date(time);
        return date;
    }

    /**
     * 根据日历类型返回年月日(yyyyMMdd)
     * @param date
     * @return String
     */
    public static String getYMDByCalendar(String aMask, Calendar date) {
        SimpleDateFormat df = new SimpleDateFormat(aMask);
        return df.format(date.getTime());
    }

    /**
     * 转换字符串为日历类型
     * @param aMask
     * @param strCalendar
     * @return String
     */
    public static Calendar convertStringToCalendar(String aMask, String strCalendar) {
        SimpleDateFormat df = null;
        Calendar calendar = Calendar.getInstance();
        df = new SimpleDateFormat(aMask);
        try {
            calendar.setTime(df.parse(strCalendar));
        } catch (ParseException e) {
            calendar = null;
        }
        return calendar;
    }

    /**
     * 获得给定日期的第一天和最后一天，如果传入的值为null，则取得当月的数据
     * @param date
     * @return
     */
    public static Date[] getFirstAndLastDate(Date date) {
        Calendar cal = Calendar.getInstance();

        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");

        if (date != null) {
            cal.setTime(date);
        }

        //当前月的最后一天
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        Date endTime = cal.getTime();
        String lastTime = datef.format(endTime) + " 23:59:59";
        //当前月的第一天
        cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        String firstTime = datef.format(beginTime) + " 00:00:00";
        //日期格式的当月第一天
        Date firstDate = convertStringToDate("yyyy-MM-dd hh:mm:ss", firstTime);
        //日期格式的当月最后一天
        Date lastDate = convertStringToDate("yyyy-MM-dd hh:mm:ss", lastTime);
        return new Date[] { firstDate, lastDate };
    }

    //返回当前时间的字符串，用默认时间格式formatType
    public static String getThisDate() {
        return getThisDate("yyyy-MM-dd HH:mm:ss");
    }

    //返回输入时间的字符串，用默认时间格式formatType
    public static String getThisDate(Date date) {
        return getThisDate("yyyy-MM-dd HH:mm:ss", date);
    }

    //返回当前时间的字符串，时间格式为输入的参数(String formatType)
    public static String getThisDate(String formatType) {
        return getThisDate(formatType, new Date());
    }

    //返回输入时间的字符串，时间格式为输入的参数(String formatType)
    public static String getThisDate(String formatType, Date date) {
        SimpleDateFormat format = new SimpleDateFormat(formatType);
        return format.format(date);
    }

    //返回时间Date,用输入的时间字符串转化,输入的时间格式为默认时间格式formatType
    public static Date getAsStringDate(String strdate){
        return getAsStringDate(strdate, "yyyy-MM-dd HH:mm:ss");
    }

    //返回时间Date,用输入的时间字符串转化,输入的时间格式为String formatType
    public static Date getAsStringDate(String strdate, String formatType) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatType);
            return format.parse(strdate);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static String getTimeDash(Date date){
        String formatType = "HH-mm-ss";
        SimpleDateFormat format = new SimpleDateFormat(formatType);
        return format.format(date);
    }
}
